export * from './lang'
export * from './http'
export * from './dataType'
export * from './caculator'

// is array
export const isArray = (arr) => {
  return Object.prototype.toString.call(arr) === '[object Array]'
}

/**
 * 从请求路径中解析出基本路径和参数信息
 * @param {string} url 请求路径
 */
export const divideUrlAndQuery = (url) => {
  url = url || ''
  const query = {}
  if(url.indexOf('?') > -1){
    var arr = url.split('?')
    url = arr[0]

    var params = arr[1] ? arr[1].split("&") : []

    for(var i = 0; i < params.length; i++){
      var item = params[i].split("=")
      query[item[0]] = item[1] || ''
    }
  }

  return {
    url: url,
    params: query
  }
}

// const bulidQuerySetting = (key, value, builder) => {
//   return {name: key, value: value, linkOpt: "and", builder: builder}
// }

// // 将高级搜索条件解析成查询字符串
// export const buildQueryString = (params) => {
//   let qs = []
//   for (let key in params) {
//     if (key == 'OPTTIME') {
//       qs.push(bulidQuerySetting(key, params[key][0] + ' 00:00:00', 'moreEqual'))
//     }

//     else {
//       if (params[key]) {
//         qs.push(bulidQuerySetting(key, params[key], 'include'))
//       }
//     }
//   }

//   return JSON.stringify(qs)
// }

/**
 * 根据菜单名称获取菜单详细信息，（利用 window.menu 对象）
 * @param {string} menuName 菜单名称
 */
export const getMenuInfo = (menuName, menus = window.menu) => {
  if (!menus) {
    return null
  }

  for(let i = 0; i < menus.length; i++) {
    const item = menus[i]
    if (item.title === menuName) {
      return {
        ...item,
        name: menuName,
        ...divideUrlAndQuery(item.path)
      }
    }
    if(item.sub){
      const ret = getMenuInfo(menuName, item.sub)
      if (ret) {
        return ret
      }
    }
  }

  return null // 获取不到，正常情况不会出现
}

/**
 * 将嵌套数组转为平铺
 */
export const flatternArr = (arr) => {
  if (!arr) {
    return []
  }

  return arr.reduce((ret, item) => {
    if (isArray(item)) {
      return [...ret, ...flatternArr(item)]
    }
    return [...ret, item]
  }, [])
}

/**
 * 序列化请求数据
 */
export const serialize = (data) => {
  if (!data) return '';
  var pairs = [], value;
  for (var name in data) {
    if (!data.hasOwnProperty(name)) continue;
    if (typeof data[name] === 'function') continue;
    value = String(data[name]);
    name = encodeURIComponent(name);
    value = encodeURIComponent(value);
    pairs.push(name + '=' + value);
  }
  if (pairs.length) {
    return pairs.join('&');
  } else {
    return ''
  }
}

export const normalizeMeta = (item) => {
  if (item.dataType === 'Timestamp' && !item.format) {
    item.format = 'yyyy-MM-dd HH:mm:ss'
  }

  item.showIntro = item.showIntro === 'true' ? true : false

  if (item.dataType === 'double' && item.xtype === 'number') {
    let digits = 2
    if (item.format) {
      const splits = item.format.split('.')
      if (splits.length === 2) {
        digits = splits[1].length
      }
    }
    item.JSONParam = JSON.stringify({ ...(item.JSONParam || {}), decimalDigits: digits })
  } else if (item.xtype === 'uploadfile') {
    const jsonParam = item.JSONParam || {}

    if (item.size) {
      jsonParam.size = item.size * 1000
    }
  
    if (item.type) {
      jsonParam.type = item.type.split(',')
    }

    item.JSONParam = jsonParam
  }

  if (item.format) {
    item.formatString = item.format

    if (item.xtype === 'date-full' && (item.format.indexOf('H') === -1)) {
      item.xtype = 'date-local'
    }
  }

  if (item.ref) {
    item.ref = item.ref.map(normalizeMeta)
  }

  if (item.xtype === 'readonly-text') {
    item.xtype = 'text'
    item.readonly = true
  }

  return item
}

const isObject = (val) => {
  return typeof val === 'object'
}

export const isNumber = (val) => {
  val = Number(val)
  return !isNaN(val)
}

const isNaNumber = (str) => {
  return typeof str === 'number' && isNaN(str)
}

// 判断两个对象是否相同
export const isSame = (oldVal, newVal) => {
  oldVal = isNaNumber(oldVal) ? 0 : oldVal
  newVal = isNaNumber(newVal) ? 0 : newVal

  if (!oldVal && !newVal) {
    return true
  }

  if ((oldVal && !newVal) || (!oldVal && newVal)) {
    // console.log(' is diffrent 0', oldVal, newVal)
    return false
  }

  for (let key in oldVal) {
    const oldData = oldVal[key]
    const newData = newVal[key]

    if (isObject(oldData) && isObject(newData) && isSame(oldData, newData)) {
      // console.log('old new object diff', oldData, newData)
      continue
    }

    // 浮点数精度问题
    if (isNumber(oldData) && (Math.abs(Number(oldData) - Number(newData)) < 0.000000001)) {
      // console.log('number same', oldData, newData)
      continue
    }

    if (oldData != newData) {
      // console.log('subs', oldData - newData)
      // console.log('is different ', oldVal, newVal)
      return false
    }
  }

  return true
}
